//
//  EnergyFinishListView.m
//  Ginix
//
//  Created by 冯志强 on 2025/3/5.
//

#import "EnergyFinishListView.h"
#import "EnergyListCell.h"
#import "EnergyShopListModel.h"

@interface EnergyFinishListView()<UICollectionViewDelegate,UICollectionViewDataSource>
@property (nonatomic,strong) UICollectionView * listView;
@property (nonatomic,strong) NSMutableArray * listArray;
@property (nonatomic,assign) NSInteger currentPage;

@end

@implementation EnergyFinishListView

-(instancetype)initWithFrame:(CGRect)frame{
    
    if (self = [super initWithFrame:frame]) {
        [self setSubViewLayout];
        self.currentPage = 1;
        [self requestData];
        [self addRefresh];
    }
    return self;
}

-(void)setSubViewLayout{
    
    [self addSubview:self.listView];
    [_listView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self).insets(UIEdgeInsetsMake(10 * FMScaleX, 0, 0, 0));
    }];
}

-(void)addRefresh{
        
   @WeakObj(self)
    [self.listView headerRefreshBlock:^{
        @StrongObj(self)
        self.currentPage = 1;
        [self requestData];
    }];
    
    [self.listView footerRefreshBlock:^{
        @StrongObj(self)
        self.currentPage ++;
        [self requestData];
    }];
}

-(void)requestData{
    @WeakObj(self)
    NSDictionary * parm = @{@"pageNub":@(self.currentPage),@"pageSize":@(10)};
    [SPNetworkingIns POST:PORT(@"monetary-center/api/monetary/getExchangeProductListByWinning") params:parm success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject, NSInteger code) {
        @StrongObj(self)
        [self.listView.mj_footer endRefreshing];
        [self.listView.mj_header endRefreshing];
        NSArray * dataArr = [[responseObject objectForKey:@"data"] objectForKey:@"list"];
        NSMutableArray * array = [NSMutableArray array];

        if (dataArr) {
            for (NSDictionary * item in dataArr) {
                EnergyShopListModel * model = [[EnergyShopListModel alloc]init];
                model = [EnergyShopListModel yy_modelWithDictionary:item];
                [array addObject:model];
            }
        }
        if (self.currentPage == 1) {
            [self.listArray removeAllObjects];
        }
        if (array.count > 0) {
            [self.listArray addObjectsFromArray:array];
        }else {
            self.currentPage --;
        }
        [self.listView reloadData];
    } fail:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nonnull error) {
        
    }];
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{

    return self.listArray.count;
}

-(__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{

    EnergyListCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"EnergyListCell" forIndexPath:indexPath];
    EnergyShopListModel * model = self.listArray[indexPath.row];
    if (model) {
        [cell setInfoModel:model];
    }
    return cell;
}
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{

    return UIEdgeInsetsMake(10* FMScaleX, 20* FMScaleX, 10* FMScaleX, 20* FMScaleX);
}

-(UICollectionView *)listView{

    if (!_listView) {
        UICollectionViewFlowLayout * layout = [[UICollectionViewFlowLayout alloc]init];
        layout.minimumLineSpacing = 10 * FMScaleX;
        layout.minimumInteritemSpacing = 10 * FMScaleX;
        layout.itemSize = CGSizeMake(158 * FMScaleX, 241 * FMScaleX);
        _listView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:layout];
        _listView.delegate = self;
        _listView.dataSource = self;
        _listView.backgroundColor = UIColor.clearColor;
        _listView.showsVerticalScrollIndicator = NO;
        _listView.showsHorizontalScrollIndicator = NO;
        [_listView registerClass:[EnergyListCell class] forCellWithReuseIdentifier:@"EnergyListCell"];
    }
    return _listView;
}
-(NSMutableArray *)listArray{
    
    if (!_listArray) {
        _listArray = [[NSMutableArray alloc]init];
    }
    return _listArray;
}

@end
